package com.seis_645.appTesting;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class DealerCompareActivity extends Activity {

	TableLayout table;
	DealerDataSource datasource;
	Spinner spinner1, spinner2;
	Dealer dealer1, dealer2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		View viewToLoad = LayoutInflater.from(this.getParent()).inflate(
				R.layout.compare_dealer, null);
		this.setContentView(viewToLoad);
		spinner1 = (Spinner) findViewById(R.id.spinner_dealer1);
		spinner2 = (Spinner) findViewById(R.id.spinner_dealer2);
		datasource = new DealerDataSource(this);
		datasource.open();
		List<Dealer> values = datasource.get_all_dealers();
		ArrayAdapter<Dealer> adapter = new ArrayAdapter<Dealer>(this,
				android.R.layout.simple_spinner_item, values);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner1.setAdapter(adapter);
		spinner2.setAdapter(adapter);

		table = (TableLayout) findViewById(R.id.updateTable);
		Button update = (Button) findViewById(R.id.btn_update_table);
		Button back = (Button) findViewById(R.id.btn_back);
		OnTouchListener updateListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					updateTable();
					// return true;
				}
				return false;
			}
		};

		OnTouchListener backListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					datasource.close();
					finish();
				}
				return false;
			}
		};

		update.setOnTouchListener(updateListener);
		back.setOnTouchListener(backListener);
	}

	private void updateTable() {

		dealer1 = (Dealer) spinner1.getSelectedItem();
		dealer2 = (Dealer) spinner2.getSelectedItem();

		for (int i = 1; i < table.getChildCount(); i++) {
			if (i < 4) {
				if (table.getChildAt(i).getId() != -1) {
					TableRow row = (TableRow) findViewById(table.getChildAt(i)
							.getId());
					int val1 = 0;
					int val2 = 0;
					switch (i) {
					case 1:
						val1 = dealer1.get_cleanliness();
						val2 = dealer2.get_cleanliness();
						break;
					case 2:
						val1 = dealer1.get_friendliness();
						val2 = dealer2.get_friendliness();
						break;
					case 3:
						val1 = dealer1.get_supply();
						val2 = dealer2.get_supply();
						break;
					}
					TextView col1 = (TextView) row.getChildAt(1);
					TextView col2 = (TextView) row.getChildAt(2);
					TextView col3 = (TextView) row.getChildAt(3);
					col1.setText(Integer.toString(val1));
					col2.setText(Integer.toString(val2));
					col3.setText(Integer.toString(val1 - val2));
				}
			}
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

}
